package com.gimi.cloud.bbp.dao.dao.prod;

import com.gimi.cloud.bbp.dao.common.SqlProvider;
import com.gimi.cloud.bbp.dao.dao.sys.BaseDao;
import com.gimi.cloud.bbp.dao.myMapper.MyMapper;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParamProvider;
import com.gimi.cloud.bbp.dto.prod.EcProdCategoryDTO;
import com.gimi.cloud.bbp.entity.prod.EcProdCategoryEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* 商品品类数据库访问层
* @author lixj
* @Description
* @create 2017-12-18
* @version 1.0
**/
@Mapper
public interface EcProdCategoryDao extends MyMapper<EcProdCategoryEntity>,BaseDao<EcProdCategoryEntity,EcProdCategoryDTO> {
    /**
     * 获取所有品类
     * @return
     */
    @Select("SELECT prod_cat_id,prod_cat_name,parent_cat_id,cat_grade FROM ec_prod_category where is_delete = 0")
    List<EcProdCategoryDTO> getAllProdCategory();

    /**
     * 根据参数查询出所有的对应商品品类列表
     * @param i
     */
    @Select("select t.* from ec_prod_category t where t.cat_grade=#{i} and is_delete=0")
    List<EcProdCategoryDTO> searchCategoryListByGrade(int i);

    @SelectProvider(method = "selectCountBySql",type = QueryParamProvider.class)
    String findMaxCatIdByGrade(QueryParam param);

    @Select("select t.* from ec_prod_category t where t.prod_cat_name=#{prodCatName} and t.cat_grade =#{catGrade} and t.prod_cat_id !=#{prodCatId} ")
    List<EcProdCategoryDTO> getCategoryListByName(@Param(value = "prodCatName")String prodCatName, @Param(value = "catGrade")Integer catGrade, @Param(value = "prodCatId")String prodCatId);

    @SelectProvider(type = SqlProvider.class,method = "sql")
    List<EcProdCategoryDTO> findByCatIds(String sql);

    @Select("select t.* from ec_prod_category t where t.prod_cat_name=#{prodCatName} and t.cat_grade =#{catGrade} ")
    List<EcProdCategoryDTO> getCategoryListByNameForAdd(@Param(value = "prodCatName")String prodCatName, @Param(value = "catGrade")Integer catGrade);
    
    /**
     * 根据父级ID获取直属商品品类
     * @return
     * @param parentCatId
     */
    @Select("select t.prod_cat_id prodCatId,t.prod_cat_name prodCatName,t.parent_cat_id parentCatId,t.cat_icon_url catIconUrl from ec_prod_category t where t.parent_cat_id=#{parentCatId} and is_delete=0 and cat_show_app=1")
    List<EcProdCategoryDTO> getChildCategoriesByParentId(@Param(value = "parentCatId") String parentCatId);

    /**
     * 根据父级ID获取从属商品品类（只能跳一级，如：传入一级id查从属二级，不能传入一级查三级下面的分类）
     * @return id集合
     * @param parentCatId
     */
    @SelectProvider(type = QueryParamProvider.class, method = "selectListBySql")
    List<String> getChildCategoriesIdByParentIdAndGrade(QueryParam queryParam);
    /**
	 * 获取直属分类的id
	 * @param firstCatId 一级分类id
	 * @return
	 */
    @Select("select cate1.prod_cat_id from ec_prod_category cate1 inner join ec_prod_category cate2 on cate1.parent_cat_id=cate2.prod_cat_id where cate2.parent_cat_id=#{firstCatId} and cate2.cat_grade=2 and cate2.is_delete=0 and cate1.is_delete=0")
	List<String> getThirdGradeCategoriesByFirstGradeCategoryId(@Param(value = "firstCatId") String firstCatId);
    
    /**
     * 根据参数查询出所有的对应商品品类列表
     * @param catGrade
     */
    @Select("select t.prod_cat_id prodCatId,t.prod_cat_name prodCatName,t.cat_icon_url catIconUrl from ec_prod_category t where t.cat_grade=#{catGrade} and is_delete=0 and cat_show_app=1")
	List<EcProdCategoryDTO> getCategoriesByGrade(int catGrade);
    
    /**
     * 根据参数查询出所有的一二级分类
     */
    @Select("select t.prod_cat_id prodCatId,t.parent_cat_id parentCatId,t.prod_cat_name prodCatName,t.cat_icon_url catIconUrl,t.cat_grade catGrade from ec_prod_category t where is_delete=0 and cat_show_app=1 and cat_grade in(1,2) order by add_time asc")
	List<EcProdCategoryDTO> getHomePageCategories();

    /**
     * 根据主键获取品类信息
     * @param prodCatId 品类id
     * @return 获取结果
     * @throws Exception
     */
    @Select("SELECT cate.*,group_concat(rela.prod_brand_id) pd_id " +
            " FROM ec_prod_category cate " +
            " LEFT JOIN ec_category_brand_rela rela ON rela.prod_cat_id = cate.prod_cat_id" +
            " WHERE cate.is_delete = 0 AND cate.prod_cat_id = #{prodCatId}")
    EcProdCategoryDTO queryProdCategoryByProdCatId(@Param("prodCatId") Object prodCatId) throws Exception;
    
}
